package com.zeng.jiudian.service.impl;

import com.zeng.jiudian.bean.ShowModel;
import com.zeng.jiudian.dao.ShowDao;
import com.zeng.jiudian.dao.impl.ShowDaoImpl;
import com.zeng.jiudian.service.ShowService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/25 - 16:28
 */
public class ShowServiceImpl implements ShowService
{
    @Override
    public int add(ShowModel showModel)
    {
        ShowDao showDao = new ShowDaoImpl();

        int count = showDao.add(showModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<ShowModel> findAll(ShowModel showModel)
    {
        ShowDao showDao = new ShowDaoImpl();

        return showDao.findAll(showModel);
    }

    @Override
    public int getCount(ShowModel showModel)
    {
        ShowDao showDao = new ShowDaoImpl();

        return showDao.getCount(showModel);
    }

    @Override
    public int delete(int id)
    {
        ShowDao showDao = new ShowDaoImpl();

        int count = showDao.delete(id);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public ShowModel findById(int id)
    {
        ShowDao showDao = new ShowDaoImpl();

        return showDao.findById(id);
    }

    @Override
    public int update(ShowModel showModel)
    {
        ShowDao showDao = new ShowDaoImpl();

        if ("".equals(showModel.getBigTitle()))
        {
            showModel.setBigTitle(null);
        }
        if ("".equals(showModel.getSmallTitle()))
        {
            showModel.setSmallTitle(null);
        }
        if ("".equals(showModel.getHotelMsg()))
        {
            showModel.setHotelMsg(null);
        }
        if ("".equals(showModel.getContentTop()))
        {
            showModel.setContentTop(null);
        }
        if ("".equals(showModel.getContentMid()))
        {
            showModel.setContentMid(null);
        }
        if ("".equals(showModel.getContentBottom()))
        {
            showModel.setContentBottom(null);
        }

        int count = showDao.update(showModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<ShowModel> findUnion()
    {
        List<ShowModel> list = null;
        Jedis jedis = new Jedis();
        jedis.select(0);
        Long length = jedis.llen("showId");
        if (length > 0)
        {
            list = new ArrayList<>();
            List<String> showIds = jedis.lrange("showId", 0, length);
            List<String> bigTitle = jedis.lrange("showBigTitle", 0, length);
            List<String> smallTitle = jedis.lrange("showSmallTitle", 0, length);
            List<String> hotelMsg = jedis.lrange("showHotelMsg", 0, length);
            List<String> contentTop = jedis.lrange("showContentTop", 0, length);
            List<String> contentMid = jedis.lrange("showContentMid", 0, length);
            List<String> contentBottom = jedis.lrange("showContentBottom", 0, length);
            List<String> createTime = jedis.lrange("showCreateTime", 0, length);
            List<String> updateTime = jedis.lrange("showUpdateTime", 0, length);
            for (int i = 0; i < length; i++)
            {
                ShowModel model = new ShowModel();
                model.setId(Integer.parseInt(showIds.get(i)));
                model.setBigTitle(bigTitle.get(i));
                model.setSmallTitle(smallTitle.get(i));
                model.setHotelMsg(hotelMsg.get(i));
                model.setContentTop(contentTop.get(i));
                model.setContentMid(contentMid.get(i));
                model.setContentBottom(contentBottom.get(i));
                model.setCreateTime(createTime.get(i));
                model.setUpdateTime(updateTime.get(i));
                list.add(model);
            }
        }
        else
        {
            ShowDao showDao = new ShowDaoImpl();
            list = showDao.findUnion();
            jedis.del("showId", "showBigTitle", "showSmallTitle", "showHotelMsg", "showContentTop", "showContentMid", "showContentBottom", "showCreateTime", "showUpdateTime");
            for (ShowModel model : list)
            {
                jedis.rpush("showId", model.getId().toString());
                jedis.rpush("showBigTitle", model.getBigTitle());
                jedis.rpush("showSmallTitle", model.getSmallTitle());
                jedis.rpush("showHotelMsg", model.getHotelMsg());
                jedis.rpush("showContentTop", model.getContentTop());
                jedis.rpush("showContentMid", model.getContentMid());
                jedis.rpush("showContentBottom", model.getContentBottom());
                jedis.rpush("showCreateTime", model.getCreateTime());
                jedis.rpush("showUpdateTime", model.getUpdateTime());
            }
        }


        return list;
    }
}
